package com.cskaoyan.controller.admin.promotion;

import com.cskaoyan.bean.BaseParamBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.promotion.bo.GrouponRulesBo;
import com.cskaoyan.bean.admin.promotion.bo.GrouponRulesCreateBo;
import com.cskaoyan.bean.admin.promotion.vo.GrouponRulesVo;
import com.cskaoyan.service.admin.PromotionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Name : GrouponController.java
 * @Time : 2021/8/12 22:02
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
@RestController
@RequestMapping("admin/groupon")
public class GrouponController {

    @Autowired
    PromotionService promotionService;

    @RequiresPermissions(value = {"*", "admin:groupon:list"}, logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo getGrouponRulesList(Integer goodsId, BaseParamBo param){
        BaseRespData grouponRule = promotionService.getGrouponRulesList(goodsId, param);
        return BaseRespVo.ok(grouponRule);
    }

    @RequiresPermissions(value = {"*", "admin:groupon:update"}, logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo updateGrouponRules(@RequestBody GrouponRulesBo grouponRulesBo){
        Integer index = promotionService.updateGrouponRules(grouponRulesBo);
        if(index == 0){
            return BaseRespVo.fail("没有察觉到修改~");
        }else if(index == 1){
            return BaseRespVo.fail("该商品不存在");
        }else{
            return BaseRespVo.ok();
        }
    }

    @RequiresPermissions(value = {"*", "admin:groupon:create"}, logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo createGrouponRules(@RequestBody GrouponRulesCreateBo grouponRulesCreateBo){
        GrouponRulesVo grouponRules = promotionService.createGrouponRules(grouponRulesCreateBo);
        if(grouponRules==null){
            return BaseRespVo.fail("没有找到这个商品~");
        }
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value = {"*", "admin:groupon:delete"}, logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo deleteGrouponRules(@RequestBody GrouponRulesBo grouponRulesBo){
        promotionService.deleteGrouponRules(grouponRulesBo);
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value = {"*", "admin:groupon:listRecord"}, logical = Logical.OR)
    @RequestMapping("listRecord")
    public BaseRespVo getListRecord(Integer goodsId, BaseParamBo param){
        BaseRespData listRecord = promotionService.getListRecord(goodsId, param);
        return BaseRespVo.ok(listRecord);
    }
}
